package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Role;
import com.yugong.entity.vo.response.RoleListResponseVO;
import com.yugong.mapper.RoleMapper;
import com.yugong.service.RoleService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/9
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 查询角色信息
     *
     * @return 角色信息
     */
    @Override
    public List<RoleListResponseVO> findRole() {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Role> roleList = roleMapper.selectList(roleLambdaQueryWrapper);
        List<RoleListResponseVO> roleListResponseVOList = new ArrayList<>();
        for (Role role : roleList) {
            RoleListResponseVO roleListResponseVO = new RoleListResponseVO();
            roleListResponseVO.setRoleNo(role.getRoleNo());
            roleListResponseVO.setRoleName(role.getRoleName());
            roleListResponseVO.setRoleCreateMan(role.getRoleCreateMan());
            String createTime = timeUtils.setDateToStrDate(role.getRoleCreateTime(), Const.YYYY_MM_DD);
            roleListResponseVO.setRoleCreateTime(createTime);
            if (role.getRoleUpdateMan() == null) {
                roleListResponseVO.setRoleUpdateMan("暂未更新");
            } else {
                roleListResponseVO.setRoleUpdateMan(role.getRoleUpdateMan());
            }
            if (role.getRoleUpdateTime() == null) {
                roleListResponseVO.setRoleUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(role.getRoleUpdateTime(), Const.YYYY_MM_DD);
                roleListResponseVO.setRoleUpdateTime(updateTime);
            }
            roleListResponseVOList.add(roleListResponseVO);
        }
        return roleListResponseVOList;
    }

    /**
     * 根据角色名查询角色信息
     *
     * @param pageNum  当前页
     * @param pageSize 当前页数据个数
     * @param roleName 角色名
     * @return 角色信息
     */
    @Override
    public PageInfo<RoleListResponseVO> findRole(Integer pageNum, Integer pageSize, String roleName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper
                .like(Role::getRoleName, roleName)
                .orderByAsc(Role::getRoleCreateTime);
        List<Role> roleList = roleMapper.selectList(roleLambdaQueryWrapper);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.like("role_name", roleName);
        Long roleCountNumber = roleMapper.selectCount(roleQueryWrapper);
        List<RoleListResponseVO> roleListResponseVOList = new ArrayList<>();
        for (Role role : roleList) {
            RoleListResponseVO roleListResponseVO = new RoleListResponseVO();
            roleListResponseVO.setRoleNo(role.getRoleNo());
            roleListResponseVO.setRoleName(role.getRoleName());
            roleListResponseVO.setRoleCreateMan(role.getRoleCreateMan());
            String createTime = timeUtils.setDateToStrDate(role.getRoleCreateTime(), Const.YYYY_MM_DD);
            roleListResponseVO.setRoleCreateTime(createTime);
            if (role.getRoleUpdateMan() == null) {
                roleListResponseVO.setRoleUpdateMan("未更新");
            } else {
                roleListResponseVO.setRoleUpdateMan(role.getRoleUpdateMan());
            }
            if (role.getRoleUpdateTime() == null) {
                roleListResponseVO.setRoleUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(role.getRoleUpdateTime(), Const.YYYY_MM_DD);
                roleListResponseVO.setRoleUpdateTime(updateTime);
            }
            roleListResponseVO.setRoleCountNumber(roleCountNumber);
            roleListResponseVOList.add(roleListResponseVO);
        }
        return PageInfo.of(roleListResponseVOList);
    }
}
